<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="@/assets/imgs/logo.png" alt="Logo" class="logo">
        <h2>爱你多亿点餐系统</h2>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username" class="inline-form-item">
          <span class="form-label">用户名：</span>
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            class="form-input"
          />
        </el-form-item>
        <el-form-item prop="password" class="inline-form-item">
          <span class="form-label">密码：</span>
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            class="form-input"
          />
        </el-form-item>
        <!-- <el-form-item prop="code" class="inline-form-item">
          <span class="form-label">验证码：</span>
          <div class="code-input">
            <el-input
              v-model="loginForm.code"
              placeholder="请输入验证码"
              prefix-icon="Key"
              class="captcha-input"
            />
            <el-image
              :src="captchaUrl"
              alt="验证码"
              class="captcha-image"
              @click="refreshCaptcha"
            />
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { User, Lock, Key } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
//const captchaUrl = ref('http://localhost:8080//code')

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    //{ min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    //{ min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      try {
        const res = await request.post('/user/login', loginForm)
        if (res.code == 200) {
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('user', JSON.stringify(res.data.user))
          ElMessage.success('登录成功')
          router.push('/home')
        } else {
          ElMessage.error(res.msg || '登录失败')
          // refreshCaptcha()
        }
      } catch (error) {
        ElMessage.error('用户名或密码错误，登录失败')
        // refreshCaptcha()
      } finally {
        loading.value = false
      }
    } else {
      console.log('验证失败:', fields)
    }
  })
}

// const refreshCaptcha = () => {
//   captchaUrl.value = `http://localhost:8080/code?time=${new Date().getTime()}`
// }

// onMounted(() => {
//   refreshCaptcha()
// })

</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1f4037 0%, #99f2c8 100%);
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(8px);
}

.login-box {
  position: relative;
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 20px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 10px;
}

.login-header h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.login-form {
  margin-top: 20px;
}

/* 新增样式：使标签和输入框同行显示 */
.inline-form-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.form-label {
  width: 70px;
  font-size: 14px;
  color: #606266;
  text-align: right;
  flex-shrink: 0;
}

.form-input {
  flex: 1;
}

.captcha-input {
  width: 180px;
  margin-right: 12px;
}

.code-input {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.captcha-img {
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
}

.login-button {
  width: 100%;
  padding: 12px;
  font-size: 16px;
}

:deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.8);
}

:deep(.el-button--primary) {
  background-color: #409EFF;
  border-color: #409EFF;
}
:deep(.el-button--primary:hover) {
  background-color: #79bbff;
  border-color: #79bbff;
}


</style>